package com.tiger.anim 
{
	import com.tiger.mgr.TickMgr;
	import flash.display.Bitmap;
	
	/**
	 * 用位图显示重写movieclip
	 * @author wmTiger
	 */
	public class BmdMovieClip extends Bitmap 
	{
		private var mCurrentFrame:int = 1;
		private var mTotalFrames:int = 1;
		private var mEnabled:Boolean = true;// 是否是活动的mc
		private var mPlaying:Boolean = false;// 是否在播放中
		private var mLooping:Boolean = false;// 是否是在循环播放中
		private var mLoopFrame:int;// 循环播放时的起始帧
		private var mLoopFrameEnd:int;// 循环播放时的结束帧
		private var mPlayOnce:Boolean = false;// 只播放一次，重复使用循环播放时的起始帧和结束帧
		private var mBmdArray:BmdArray;
		
		public function BmdMovieClip(bmdArr:BmdArray) 
		{
			mBmdArray = bmdArr;
			mTotalFrames = mBmdArray.length;
		}
		
		public function get currentFrame():int
		{
			return mCurrentFrame;
		}
		
		public function get totalFrames():int
		{
			return mTotalFrames;
		}
		
		public function get enabled():Boolean
		{
			return mEnabled;
		}
		
		public function play():void
		{
			mCurrentFrame = (mCurrentFrame == 1 ? 0 : mCurrentFrame - 1);// 初始化时，回退一帧
			mPlaying = true;
			TickMgr.instance.addListener(_play);
		}
		private function _play(count:int):void
		{
			nextFrame();
		}
		
		public function stop():void
		{
			showFrame();
			TickMgr.instance.removeListener(_play);
			mPlaying = mLooping = mPlayOnce = false;
		}
		
		public function gotoAndPlay(frame:Object):void
		{
			mCurrentFrame = getFrame(frame);
			play();
		}
		
		private function getFrame(frame:Object):int
		{
			var f:int = 1;
			if (frame is int) 
			{
				f = int(frame);
			}
			else if (frame is String) 
			{
				f = mBmdArray.getFrameByLabel(String(frame));
			}
			return f;
		}
		
		public function gotoAndStop(frame:Object):void
		{
			mCurrentFrame = getFrame(frame);
			stop();
		}
		
		public function nextFrame():void
		{
			mCurrentFrame = (mCurrentFrame == mTotalFrames ? 1 : mCurrentFrame + 1);
			if (mLooping) 
			{
				if (mCurrentFrame > mLoopFrameEnd) 
				{
					mCurrentFrame = (mLoopFrame - 1 <= 0 ? 1 : mLoopFrame -1);
				}
				//trace("mLooping",mCurrentFrame , mLoopFrameEnd)
			}
			if (mPlayOnce) 
			{
				if (mCurrentFrame >= mLoopFrameEnd) 
				{
					stop();
					return;
				}
				//trace("mPlayOnce",mCurrentFrame , mLoopFrameEnd)
			}
			showFrame();
		}
		
		private function showFrame():void
		{
			var element:BmdElement = mBmdArray.getElementByIdx(mCurrentFrame - 1);
			//trace("idx:",mCurrentFrame - 1);
			if (element == null) 
			{
				return;
			}
			this.bitmapData = element.bmd;
			this.x = element.offsetX;
			this.y = element.offsetY;
		}
		
		public function prevFrame():void
		{
			mCurrentFrame = (mCurrentFrame == 1 ? mTotalFrames : mCurrentFrame - 1);
			showFrame();
		}
		
		/**
		 * 在两个帧之间循环播放
		 * @param	f1		开始帧
		 * @param	f2		结束帧，并且到了此帧要跳回开始帧继续播放
		 */
		public function loopBetween2Frame(f1:Object, f2:Object):void
		{
			mCurrentFrame = mLoopFrame = getFrame(f1);
			mLoopFrameEnd = getFrame(f2);
			setPlayType("loop");
			play();
		}
		
		/**
		 * 在两个帧之间播放，播放到设定的结束帧，停止播放
		 * @param	f1		开始帧
		 * @param	f2		结束帧，并且到了此帧要停止播放
		 */
		public function playOnceBetween2Frame(f1:Object, f2:Object):void
		{
			mCurrentFrame = mLoopFrame = getFrame(f1);
			mLoopFrameEnd = getFrame(f2);
			setPlayType("once");
			play();
		}
		
		private function setPlayType(type:String):void
		{
			if (type == "loop") 
			{
				mLooping = true;
				mPlayOnce = false;
			}
			else if (type == "once") 
			{
				mLooping = false;
				mPlayOnce = true;
			}
		}
		
	}

}